﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

using Utils;

namespace System
{
    public static class Error
    {
        public static CustomException SystemError(string message = null)
        {
            return new CustomException((int)ErrorType.SystemError, message ?? (ErrorType.GeneralError.GetDescription()));
        }
        /// <summary>
        /// 默认错误
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static CustomException DefaultError(string message = null)
        {
            return new CustomException((int)ErrorType.GeneralError, message ?? (ErrorType.GeneralError.GetDescription()));
        }

        /// <summary>
        /// 参数错误
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static CustomException ArgumentsError(Dictionary<string, string> dict)
        {
            return new CustomException(dict, (int)ErrorType.ArgumentsError, dict.Values.FirstOrDefault());
        }
        /// <summary>
        /// 自定义错误
        /// </summary>
        /// <param name="enum"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static CustomException CustomError(ErrorType @enum, string message = null)
        {
            return new CustomException(Convert.ToInt32(@enum), message ?? (@enum.GetDescription()));
        }

        /// <summary>
        /// 自定义错误
        /// </summary>
        /// <param name="enum"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static CustomException CustomError(int code, string message)
        {
            return new CustomException(code, message ?? "自定义错误");
        }


        public static void PrintStackTrace(this Exception e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"[{DateTime.Now}]:{e.Message}");
            Console.WriteLine($"{e.StackTrace}");
            Console.ResetColor();
        }
    }
    public class CustomException : ApplicationException
    {
        public int Code { get; private set; }

        public Dictionary<string, string> Errors { get; private set; }

        public CustomException(int code, string message)
          : base(message)
        {
            this.Code = code;
            HResult = code;
        }

        public CustomException(Dictionary<string, string> errors, int code, string message) : this(code, message)
        {
            Errors = errors;
        }
    }


    public enum ErrorType : int
    {
        /// <summary>
        /// 系统错误
        /// </summary>
        [Description("系统错误！")]
        SystemError = 1,
        /// <summary>
        /// 参数错误
        /// </summary>
        [Description("参数有误！")]
        ArgumentsError = 1001,
        /// <summary>
        /// 一般错误
        /// </summary>
        [Description("网络繁忙，请稍后再试！")]
        GeneralError = 2001,

        /// <summary>
        /// 授权错误 出现跳转
        /// </summary>
        [Description("请重新登录！")]
        AuthorizationError = 3001,

        /// <summary>
        /// 无权限访问
        /// </summary>
        [Description("无权限访问！")]
        NoPermissionError = 3002,
        /// <summary>
        /// 返回上一步
        /// </summary>
        [Description("资源不存在！")]
        NotFound = 3003
    }
}

